import 'package:android_intent_plus/android_intent.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:url_launcher/url_launcher.dart';

import '../AppConfig/Config.dart';
import '../AppConfig/RouteAct.dart';
import '../AppConfig/ShowDialog.dart';
import '../Order/WaybillDetails/NoInvoicingSpecified.dart';
import 'package:platform/platform.dart';
class ContactedDriverList extends StatefulWidget {
  const ContactedDriverList({super.key, required this.dataList, required this.goodsMap});
  final List dataList;
  final Map goodsMap;
  @override
  State<ContactedDriverList> createState() => _ContactedDriverListState();
}

class _ContactedDriverListState extends State<ContactedDriverList> {
  _makePhoneCall(String phoneNumber) async {
    if (LocalPlatform().isAndroid) {
      var status = await Permission.phone.status;
      if (status.isGranted) {
        // 权限已被授予
        // callPhone();
        final AndroidIntent intent = AndroidIntent(
          action: 'android.intent.action.CALL',
          data: Uri.encodeFull('tel:$phoneNumber'),
        );
        await intent.launch();
      } else if (status.isDenied) {
        // 权限被拒绝，但用户可以再次授权
        showCallPhoneFirstDialog(context);
        status = await Permission.phone.request();
        if (status.isGranted) {
          // 用户同意了权限
          // callPhone();
          final AndroidIntent intent = AndroidIntent(
            action: 'android.intent.action.CALL',
            data: Uri.encodeFull('tel:$phoneNumber'),
          );
          await intent.launch();
        } else {
          showCallPhoneDialog(context);
          // 用户拒绝权限，无法发起电话
          // 可以在此处展示信息提示用户需要权限
        }
      } else if (status.isPermanentlyDenied) {
        // 权限被永久拒绝，需要用户手动开启权限
        showCallPhoneDialog(context);
      }

    }else{
      final url =Uri.parse('tel:$phoneNumber');
      if (await canLaunchUrl(url)) {
        await launchUrl(url);
      } else {
        throw '无法拨打电话：$url';
      }
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.white,
        title: Text("已联系司机",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w300),),
      ),
      body: ListView(
        children: [
          Container(
            margin: EdgeInsets.only(top: 8.h,right: 10.w,left: 10.w),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10))),
            padding: EdgeInsets.only(top: 17.h,bottom: 10.h,left: 20.w,right: 13.w),
            child:  Column(
              children: [



            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(width: 18.w,),
                Column(
                  children: [
                    Image.asset("lib/Images/OrderList/装货点icon.png",
                      width: 20.w, height: 20.h,),
                    Image.asset(
                      "lib/Images/OrderList/垂直虚线.png", width: 18.w,
                      height: 20.h,),
                    if(widget.goodsMap['loadPlaceInfo'].length == 2)
                      Column(
                        children: [
                          Image.asset(
                            "lib/Images/OrderList/装货点icon.png",
                            width: 20.w, height: 20.h,),
                          Image.asset(
                            "lib/Images/OrderList/垂直虚线.png",
                            width: 18.w, height: 20.h,),
                        ],
                      ),
                    if( widget.goodsMap['unloadPlaceInfo'].length == 2)
                      Column(
                        children: [
                          Image.asset(
                            "lib/Images/OrderList/卸货点icon.png",
                            width: 20.w, height: 20.h,),
                          Image.asset(
                            "lib/Images/OrderList/垂直虚线.png",
                            width: 18.w, height: 20.h,),
                        ],
                      ),

                    Image.asset("lib/Images/OrderList/卸货点icon.png",
                      width: 20.w, height: 20.h,),
                  ],
                ),
                SizedBox(width: 8.w,),

                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // SizedBox(height: 8.h,),
                    Row(
                      children: [
                        Text(
                          widget.goodsMap["loadPlaceInfo"][0]["city"] +
                              " " +
                              widget.goodsMap["loadPlaceInfo"][0]["county"],
                          style: TextStyle(
                              color: Colors.black, fontSize: 14.sp),),
                        SizedBox(width: 8.w,),
                        Container(
                          width: 140.w,
                          child:    Text(widget.goodsMap["returnTime"]["loadTime"],overflow: TextOverflow.ellipsis, style: TextStyle(
                              color: Color.fromRGBO(154, 154, 154, 1),
                              fontSize: 12.sp),),
                        ),

                        if(widget.goodsMap['category'] == 1)
                          BrnTagCustom(
                            tagText: "企业",
                            fontSize: 12.sp,
                            backgroundColor: MyColors.themeColor,
                            fontWeight: FontWeight.w600,
                          ),
                        if(widget.goodsMap['description'] == 1)
                          BrnTagCustom(
                            tagText: "专票",
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w600,
                          ),

                      ],
                    ),

                    Text(
                      widget.goodsMap["loadPlaceInfo"][0]["detailedAddress"],
                      style: TextStyle(
                          color: Color.fromRGBO(154, 154, 154, 1),
                          fontSize: 12.sp),),
                    SizedBox(height: 8.h,),
                    if(widget.goodsMap["loadPlaceInfo"].length == 2)
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Text(
                                widget.goodsMap["loadPlaceInfo"][1]["city"] +
                                    " " +
                                    widget.goodsMap["loadPlaceInfo"][1]["county"],
                                style: TextStyle(color: Colors.black,
                                    fontSize: 14.sp),),
                            ],
                          ),

                          Text(
                            widget.goodsMap["loadPlaceInfo"][1]["detailedAddress"],
                            style: TextStyle(color: Color.fromRGBO(
                                154, 154, 154, 1), fontSize: 12.sp),),
                          SizedBox(height: 8.h,),
                        ],
                      ),

                    Row(
                      children: [
                        Text(
                          widget.goodsMap["unloadPlaceInfo"][0]["city"] +
                              " " +
                              widget.goodsMap["unloadPlaceInfo"][0]["county"],
                          style: TextStyle(
                              color: Colors.black, fontSize: 14.sp),),
                        SizedBox(width: 8.w,),
                        if(widget.goodsMap["unloadPlaceInfo"].length ==
                            1)
                          if(widget.goodsMap["returnTime"]["unloadTime"] !=null)
                            Text(
                              widget.goodsMap["returnTime"]["unloadTime"],
                              style: TextStyle(color: Color.fromRGBO(
                                  154, 154, 154, 1), fontSize: 12.sp),),
                      ],
                    ),

                    Text(
                      widget.goodsMap["unloadPlaceInfo"][0]["detailedAddress"],
                      style: TextStyle(
                          color: Color.fromRGBO(154, 154, 154, 1),
                          fontSize: 12.sp),),
                    if(widget.goodsMap["unloadPlaceInfo"].length == 2)
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(height: 8.h,),
                          Row(
                            children: [
                              Text(
                                widget.goodsMap["unloadPlaceInfo"][1]["city"] +
                                    " " +
                                    widget.goodsMap["unloadPlaceInfo"][1]["county"],
                                style: TextStyle(color: Colors.black,
                                    fontSize: 14.sp),),
                              SizedBox(width: 8.w,),
                              Text(
                                widget.goodsMap["returnTime"]["unloadTime"],
                                style: TextStyle(color: Color.fromRGBO(
                                    154, 154, 154, 1),
                                    fontSize: 12.sp),),
                            ],
                          ),

                          Text(
                            widget.goodsMap["unloadPlaceInfo"][1]["detailedAddress"],
                            style: TextStyle(color: Color.fromRGBO(
                                154, 154, 154, 1), fontSize: 12.sp),),
                        ],
                      ),

                  ],

                ),
                //
              ],
            ),

        SizedBox(height: 8.h,),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                SizedBox(width: 17.w,),
                if( widget.goodsMap["cargoInfo"][0]["cargoVolume"]!=null&&widget.goodsMap["cargoInfo"][0]["cargoWeight"]!=null)
                  Text(widget.goodsMap["cargoInfo"][0]["cargoName"] +
                      "," +
                      widget.goodsMap["cargoInfo"][0]["cargoWeight"]
                          .toString() + "吨" + ","
                      +
                      widget.goodsMap["cargoInfo"][0]["cargoVolume"]
                          .toString() + "方" + "," +
                      widget.goodsMap["cargoInfo"][0]["packgeType"],
                    style: TextStyle(
                        color: Colors.black, fontSize: 13.sp),),
                if( widget.goodsMap["cargoInfo"][0]["cargoVolume"]==null&&widget.goodsMap["cargoInfo"][0]["cargoWeight"]!=null)
                  Text(widget.goodsMap["cargoInfo"][0]["cargoName"] +
                      "," +
                      widget.goodsMap["cargoInfo"][0]["cargoWeight"]
                          .toString() + "吨" + "," +
                      widget.goodsMap["cargoInfo"][0]["packgeType"],
                    style: TextStyle(
                        color: Colors.black, fontSize: 13.sp),),
                if( widget.goodsMap["cargoInfo"][0]["cargoVolume"]!=null&&widget.goodsMap["cargoInfo"][0]["cargoWeight"]==null)
                  Text(widget.goodsMap["cargoInfo"][0]["cargoName"] +
                      "," +
                      widget.goodsMap["cargoInfo"][0]["cargoVolume"]
                          .toString() + "方" + "," +
                      widget.goodsMap["cargoInfo"][0]["packgeType"],
                    style: TextStyle(
                        color: Colors.black, fontSize: 13.sp),),
              ],
            ),
            Text(widget.goodsMap["createTime"].substring(5, 16),
              style: TextStyle(
                  color: MyColors.textColor, fontSize: 13.sp),),
          ],
        ),
        Row(
          children: [
            SizedBox(width: 17.w,),
            !widget.goodsMap["vehicleLength"].contains(',') ?
            Text(widget.goodsMap["vehicleLength"] + "米",
              style: TextStyle(
                  color: Color.fromRGBO(51, 51, 51, 1), fontSize: 13.sp),) :
            Text(widget.goodsMap["vehicleLength"].replaceAll(
                ',', '/米') + "米", style: TextStyle(
                color: Colors.black, fontSize: 13.sp),),
            Text("丨", style: TextStyle(
                color: Color.fromRGBO(51, 51, 51, 1), fontSize: 13.sp),),
            !widget.goodsMap["vehicleType"].contains(',') ?
            Text(widget.goodsMap["vehicleType"], style: TextStyle(
                color: Color.fromRGBO(51, 51, 51, 1), fontSize: 13.sp),) :
            Text(
              widget.goodsMap["vehicleType"].replaceAll(',', '/'),
              style: TextStyle(
                  color: Color.fromRGBO(51, 51, 51, 1), fontSize: 13.sp),),
          ],
        ),
      ],
            ),
    ),
          Container(
            height: widget.goodsMap["driverContactSize"] * 100.h,
            child: ListView.builder(
                physics: NeverScrollableScrollPhysics(),
                shrinkWrap: false,
                itemCount: widget.goodsMap["returnDriverContact"].length,
                itemBuilder: (BuildContext context,
                    int inPath) {
                  return Container(
                    margin: EdgeInsets.only(
                        top: 8.h, left: 15.w, right: 15.w),
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(10))),
                    padding: EdgeInsets.all(10.w),
                    child: Column(

                      children: [

                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Image.asset("lib/Images/login/抖音登录.png",
                                  width: 40.w, height: 40.h,),
                                SizedBox(width: 8.w,),
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Row(
                                      children: [

                                        Text(widget.goodsMap["returnDriverContact"][inPath]['driverName'],
                                          style: TextStyle(
                                              color: Colors.black, fontSize: 13.sp),),
                                        // Row(
                                        //   children: List.generate(index + 2, (starIndex) {
                                        //     return Icon(
                                        //         Icons.star, color: Colors.yellow, size: 20.0);
                                        //   }),
                                        // ),
                                      ],
                                    ),
                                    Row(
                                      children: [
                                        BrnTagCustom(
                                          tagText: widget.goodsMap["returnDriverContact"][inPath]['vehicleNum'],
                                          backgroundColor: MyColors.backgreyColor,
                                          textColor: Colors.black,
                                          maxWidth: 40.w,
                                          fontSize: 10.sp,
                                        ),
                                        BrnTagCustom(
                                          tagText: widget.goodsMap["returnDriverContact"][inPath]['vehicleLength']+"米",
                                          backgroundColor: MyColors
                                              .backgreyColor,
                                          textColor: Colors
                                              .black,
                                          maxWidth: 40.w,
                                          fontSize: 10.sp,
                                        ),
                                        BrnTagCustom(
                                          tagText: widget.goodsMap["returnDriverContact"][inPath]['vehicleType'],
                                          backgroundColor: MyColors.backgreyColor,
                                          textColor: Colors.black,
                                          fontSize: 10.sp,
                                          maxWidth: 40.w,
                                        ),
                                      ],
                                    ),

                                  ],
                                ),
                              ],
                            ),


                            Column(
                              crossAxisAlignment: CrossAxisAlignment
                                  .end,
                              children: [
                                Container(
                                  width: 100.w,
                                  child:Text(widget.goodsMap["returnDriverContact"][inPath]['createTime'], style: TextStyle(overflow: TextOverflow.ellipsis, color: MyColors.textColor, fontSize: 12.sp),),
                                ),
                                Container(
                                  width: 50.w,
                                  height: 15.h,
                                  color: Color.fromRGBO(251, 236, 228, 1),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Image.asset("lib/Images/OrderList/拨入拨出电话icon.png", width: 12.w, height: 12.h,),
                                      Text("拨出", style: TextStyle(color: MyColors.themeColor, fontSize: 10.sp),)
                                    ],
                                  ),
                                ),
                              ],

                            ),
                          ],
                        ),
                        SizedBox(height: 8.h,),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Image.asset("lib/Images/OrderList/IMicon.png", width: 24.w, height: 24.h,),
                            Row(
                              children: [
                                BrnSmallMainButton(
                                  title: '指定承运',
                                  textColor: Colors.white,
                                  bgColor: MyColors.themeColor,
                                  radius: 30,
                                  fontSize: 13.sp,
                                  width: 65.w,
                                  onTap: () {
                                    Navigator.push( context, FadeRoute(page:NoInvoicingSpecified( carMap: widget.goodsMap["returnDriverContact"][inPath], goodsMap: widget.goodsMap,)));

                                  },
                                ),
                                SizedBox(width: 8.w,),
                                BrnSmallOutlineButton(
                                  title: '电话联系',
                                  textColor: MyColors.themeColor,
                                  lineColor: MyColors.themeColor,
                                  radius: 30,
                                  width: 65.w,
                                  onTap: () {
                                    _makePhoneCall(widget.goodsMap["returnDriverContact"][inPath]['phoneNum']);
                                  },
                                )
                              ],
                            ),
                          ],
                        )
                      ],
                    ),
                  );
                }
            ),
          ),
    ],
      ),
    );
  }
}
